package com.laiyizhan.cms.first.service.impl;

import com.laiyizhan.cms.first.request.SecondRequest;
import com.laiyizhan.cms.first.service.IFirstService;
import com.laiyizhan.cms.first.service.ISecondService;
import com.laiyizhan.common.mapper.*;
import com.laiyizhan.common.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

import static org.springframework.beans.BeanUtils.copyProperties;
import static org.springframework.beans.BeanUtils.findDeclaredMethod;

/**
 * Created by SoulWolf on 2017/7/19.
 */
@Service
public class SecondServiceImpl implements ISecondService{

    @Autowired
    Second_aMapper second_aMapper;
    @Autowired
    Second_messageMapper second_messageMapper;
    @Autowired
    Second_bMapper second_bMapper;
    @Autowired
    Second_cMapper second_cMapper;
    @Autowired
    Second_dMapper second_dMapper;
    @Autowired
    Second_eMapper second_eMapper;
    @Autowired
    Second_fMapper second_fMapper;
    @Autowired
    Second_gMapper second_gMapper;
    @Autowired
    StateMapper stateMapper;

    public State Second_index(SecondRequest request) {
        State temp = stateMapper.selectByName(request.getoName());
        if (temp == null) {
            State state = new State();
            state.setHospital(request.getoName());
            state.setA("0");
            state.setB("0");
            state.setC("0");
            state.setD("0");
            state.setE("0");
            state.setF("0");
            state.setG("0");
            stateMapper.insertSelective(state);
            return state;
        }else {
            return stateMapper.selectByName(request.getoName());
        }
    }


    private String[] a_h = {"年平均在职职工人数（人）","卫生技术人员（人）","其他技术人员（人）","管理人员（人）","工勤技能人员 (人)","临时工（人）（选填）","在职职工年人均工资（万元/人）"};

    private String[] c_c = {"基本公共卫生服务项目资金总量（万元）","一、建立居民健康档案","二、健康教育","三、预防接种",
            "四、儿童健康管理","五、孕产妇健康管理","六、老年人健康管理","七、慢性病患者健康管理（高血压）",
            "七、慢性病患者健康管理（高血压）","九、重症精神疾病患者管理","十、结核病患者健康管理",
            "十一、中医药健康管理","十二、传染病和突发公共卫生事件报告和处理","十三、卫生计生监督协管"};

    private String[] e_h = {"年度收费总额（元）","服务量（人次）","次均收费水平（元/人次）","卫生材料支出（元）","药品支出（元）"};
    private String[] d_c = {"各级政府","国际组织","国际社会组织"};



    public String Second_a(SecondRequest request) {
        Check_message("a",request.getoName());
         /*存入个人信息*/
        Second_message second_message = new Second_message();
        copyProperties
                (request,second_message);
        second_messageMapper.insertSelective(second_message);


        /*数据处理*/
        float[] count=new float[7];
        for(int i =1;i<7;i++){
            if(request.getSecondA()[i]!=""){
                count[i]=Float.parseFloat(request.getSecondA()[i]);
            }
            if(i<5){count[0]=count[0]+count[i];}
        }
        if(count[0]!=0 || count[5]!=0){
            if(count[6]==0){
                return "no";
            }
        }

           /*生成对象，存入数据库*/
        List<Second_a> list = new ArrayList<Second_a>();
        for(int i=0;i<7;i++) {
            Second_a second_a = new Second_a();
            second_a.setHospital(request.getoName());
            second_a.setType(a_h[i]);
            second_a.setCount(String .valueOf(count[i]));
            list.add(second_a);
        }
        second_aMapper.insertBatch(list);
        Change_state("a",request.getoName());
        return "ok";
    }


    public String Second_b(SecondRequest request){
        Check_message("b",request.getoName());
        /*无需数据处理，直接放入数据库*/
        List<Second_b> list = new ArrayList<Second_b>();
        for(int i=0;i<100;i=i+2){
            Second_b second_b = new Second_b();
            second_b.setHospital(request.getoName());
            second_b.setSum(request.getSecondB()[100]);
            second_b.setSelf(request.getSecondB()[101]);
            if(request.getSecondB()[i]!=""){
                second_b.setType(request.getSecondB()[i]);
                second_b.setCount(request.getSecondB()[i+1]);
                list.add(second_b);
            }
        }
        if(list.size()!=0){second_bMapper.insertBatch(list);}
        Change_state("b",request.getoName());
        return "ok";

    }

    public String Second_c(SecondRequest request) {
        Check_message("c",request.getoName());
        /*数据处理*/
        float[] count =new float[28];
            for (int i = 0; i < 28; i=i+2) {
                if(request.getSecondC()[i]!=""){
                    count[i] = Float.parseFloat(request.getSecondC()[i]);
             }
                count[0] = count[0] + count[i];
                request.getSecondC()[i] = String.valueOf(count[i]);
            }


        /*放入数据库*/
        List<Second_c> list = new ArrayList<Second_c>();
        for(int j=0;j<28;j=j+2){

            Second_c second_c = new Second_c();
            second_c.setHospital(request.getoName());
            second_c.setType(c_c[j/2]);
            if(j!=0){second_c.setCount(request.getSecondC()[j]);}
            else{second_c.setCount(String.valueOf(count[0]));}
            second_c.setRemarks(request.getSecondC()[j+1]);
            list.add(second_c);
        }
        second_cMapper.insertBatch(list);
        Change_state("c",request.getoName());
        return "ok";
    }

    public String Second_e(SecondRequest request){
        Check_message("e",request.getoName());
        /*所有必填数据初始化为0*/
        for(int i=0;i<96;i++){
            if((i%6)!=0 && request.getSecondE()[i]==""){
                request.setSecondEindex("0.0",i);
            }
            if(i<16  && request.getSecondEl()[i]==""){
                request.setSecondElindex("0.0",i);
            }
        }

        /*放入数据库*/
        List<Second_e> list = new ArrayList<Second_e>();
        for(int i=0;i<120;i=i+6){
            for(int j=0;j<5;j++) {
                Second_e second_e = new Second_e();
                second_e.setHospital(request.getoName());
                second_e.setType(request.getSecondE()[i]);
                second_e.setcType(e_h[j]);
                second_e.setCount(request.getSecondE()[i+j+1]);
                list.add(second_e);
            }
            Second_e second_e = new Second_e();
            second_e.setHospital(request.getoName());
            second_e.setType(request.getSecondE()[i]);
            second_e.setcType("其他支出（元）");
            second_e.setCount(request.getSecondEl()[i/6]);
            list.add(second_e);
        }
        second_eMapper.insertBatch(list);
       Change_state("e",request.getoName());
        return "ok";

    }


    public String Second_d(SecondRequest request){
        Check_message("d",request.getoName());
        /*数据处理*/
        float d_count=0F;
        for(int i=1;i<32;i=i+2){
            if(request.getSecondD()[i]!=""){
                d_count=d_count+Float.parseFloat(request.getSecondD()[i]);
            }
        }


        List<Second_d> list = new ArrayList<Second_d>();
        if(request.getSecondD()!=null){
            request.getSecondD()[1]=String.valueOf(d_count);
            for(int j=0;j<52;j=j+2){
                Second_d Second_d = new Second_d();
                /*医院名称固定，由前一页传递过来*/
                Second_d.setHosptial(request.getoName());
                if(j<32){
                    Second_d.setSource(d_c[0]);
                }else if(j<42){
                    Second_d.setSource(d_c[1]);
                }else {
                    Second_d.setSource(d_c[2]);
                }
                Second_d.setProjectName(request.getSecondD()[j]);
                Second_d.setCount(request.getSecondD()[j+1]);
                list.add(Second_d);
            }
            second_dMapper.insertBatch(list);
           Change_state("d",request.getoName());
        }
        return "ok";
    }



    public String Second_f(SecondRequest request) {
        Check_message("f",request.getoName());
        List<Second_f> list = new ArrayList<>();
        if(request.getSecondF()!=null){
            for(int i=0;i<40;i=i+2){
                Second_f secend_f = new Second_f();
                secend_f.setHospital(request.getoName());
                secend_f.setName(request.getSecondF()[i]);
                if (i < 30) {
                    if(request.getSecondF()[i+1]!=""){secend_f.setCount(request.getSecondF()[i+1]);}
                    else{secend_f.setCount("0");}
                }else{
                    secend_f.setCount(request.getSecondF()[i+1]);
                }

                list.add(secend_f);
            }
        }
        second_fMapper.insertBatch(list);
        Change_state("f",request.getoName());
        return "ok";
    }


    public String Second_g(SecondRequest request) {
        Check_message("g",request.getoName());
        List<Second_g> list = new ArrayList<>();

        if(request.getSecondG()!=null){
            for(int i=0;i<244;i=i+4){
                Second_g second_g = new Second_g();
                second_g.setHospital(request.getoName());
                second_g.setName(request.getSecondG()[i]);
                second_g.setType(request.getSecondG()[i+1]);
                /*把必须填的数，没填的默认为0*/
                if (i < 44) {
                    if(request.getSecondG()[i+2]!=""){
                        second_g.setCount(request.getSecondG()[i+2]);
                    } else{
                        second_g.setCount("0.0");
                    }
                    if(request.getSecondG()[i+3]!="") {
                        second_g.setPrice(request.getSecondG()[i + 3]);
                    }else{
                        second_g.setPrice("0.0");
                    }
                }else{
                    second_g.setCount(request.getSecondG()[i+2]);
                    second_g.setPrice(request.getSecondG()[i + 3]);
                }
                    list.add(second_g);
            }

        }
        second_gMapper.insertBatch(list);
        Change_state("g",request.getoName());
        return "ok";
    }



    public String Check_message(String table,String oName){
        switch (table)
        {
            case "a":
                if(second_aMapper.selectByName(oName)!=null){
                    second_aMapper.deleteByName(oName);
                }
                break;
            case "b":
                if(second_bMapper.selectByName(oName)!=null){
                    second_bMapper.deleteByName(oName);
                }
                break;
            case "c":
                if(second_cMapper.selectByName(oName)!=null){
                    second_cMapper.deleteByName(oName);
                }
                break;
            case "d":
                if(second_dMapper.selectByName(oName)!=null){
                    second_dMapper.deleteByName(oName);
                }
                break;
            case "e":
                if(second_eMapper.selectByName(oName)!=null){
                    second_eMapper.deleteByName(oName);
                }
                break;
            case "f":
                if(second_fMapper.selectByName(oName)!=null){
                    second_fMapper.deleteByName(oName);
                }
                break;
            case "g":
                if(second_gMapper.selectByName(oName)!=null){
                    second_gMapper.deleteByName(oName);
                }
                break;
        }

        return "ok";
    }



    private String Change_state(String table,String oName) {
        State state = stateMapper.selectByName(oName);
        switch (table)
        {
            case "a":
                state.setA("1");
                break;
            case "b":
                state.setB("1");
                break;
            case "c":
                state.setC("1");
                break;
            case "d":
                state.setD("1");
                break;
            case "e":
                state.setE("1");
                break;
            case "f":
                state.setF("1");
                break;
            case "g":
                state.setG("1");
                break;
        }
        stateMapper.updateByName(state);
        return "ok";
    }

}
